/*
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx;

import wtkx.be.Function;
import wtkx.io.UrlcInputStream;

import java.io.IOException;
import java.net.URL;

/**
 * Currently all of the elements in this package display as blocks.
 * Forthcoming alternatives will include table and inline.
 *  
 * As a container for inline elements including {@link Text}, {@link A
 * Anchor} and {@link Img} children, the P element will accept the
 * additions of character text and elements for inline display.
 * 
 * @author jdp
 */
public class P
    /*
     * extends Container
     */
    extends Text
{


    protected URL src;


    public P() {
        super();
        this.auto = true;
        this.flowed = true;
        this.border.none();
        this.setView(wtkx.ui.View.Vertical);
    }


    public final URL getSrc() {
        return src;
    }
    public final void setSrc(URL src) {
        if (null != src)
            this.src = src;
        else
            throw new IllegalArgumentException();
    }
    public final void setSrc(String string){
        URL url = this.getInputURL(string);
        this.setSrc(url);
    }
    public void layout(){

        if (1 > this.getCharacterCount())
            this.reload();

        super.layout();
    }
    public boolean reload(){

        URL src = this.src;
        if (null != src){
            try {
                UrlcInputStream in = this.reader(this,src.openConnection());
                if (null != in){
                    try {
                        String string = in.readString();

                        this.setText(string);

                        this.invalidate();

                        return true;
                    }
                    finally {
                        in.close();
                    }
                }
            }
            catch (Function.Error exc){
                exc.printStackTrace();
            }
            catch (IOException exc){
                exc.printStackTrace();
            }
        }
        return false;
    }
}
